package com.itsu.app.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.itsu.api.controllerApi.IMControllerApi;
import com.itsu.app.service.AppIMService;
import com.itsu.pojo.enums.CommentType;
import com.itsu.pojo.vo.PageResult;
import com.itsu.pojo.vo.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@Slf4j
public class IMController implements IMControllerApi {

    @Autowired
    AppIMService appIMService;

    @Override
    public ResponseEntity queryUserInfoByUserName(String huanxinId) {
        UserInfoVo vo =appIMService.queryUserInfoByUserId(StrUtil.subAfter(huanxinId, "_", true));
        return ResponseEntity.ok(vo);
    }

    @Override
    public ResponseEntity contacts(Map<String, String> map) {
        String userId = map.get("userId");
        appIMService.contacts(Convert.toInt(userId));
        return ResponseEntity.ok(null);
    }

    @Override
    public ResponseEntity findContacts(Map<String, String> map) {
        String page = map.get("page");
        String pageSize = map.get("pagesize");
        String keyword = map.get("keyword");
        PageResult result = appIMService.findContacts(Convert.toInt(page), Convert.toInt(pageSize), keyword);
        return ResponseEntity.ok(result);
    }

    @Override
    public ResponseEntity findLikesUsers(Map<String, String> map) {
        String page = map.get("page");
        String pageSize = map.get("pagesize");
        PageResult result = appIMService.getComments(Convert.toInt(page), Convert.toInt(pageSize), CommentType.LIKED.getValue());
        return ResponseEntity.ok(result);
    }

    @Override
    public ResponseEntity findLovesUsers(Map<String, String> map) {
        String page = map.get("page");
        String pageSize = map.get("pagesize");
        PageResult result = appIMService.getComments(Convert.toInt(page), Convert.toInt(pageSize), CommentType.LOVED.getValue());
        return ResponseEntity.ok(result);
    }

    @Override
    public ResponseEntity findCommentsUsers(Map<String, String> map) {
        String page = map.get("page");
        String pageSize = map.get("pagesize");
        PageResult result = appIMService.getComments(Convert.toInt(page), Convert.toInt(pageSize), CommentType.COMMENT.getValue());
        return ResponseEntity.ok(result);
    }

    @Override
    public ResponseEntity findAnnouns(Map<String, String> map) {
        String page = map.get("page");
        String pageSize = map.get("pagesize");
        PageResult result = appIMService.getAnnouns(Convert.toInt(page), Convert.toInt(pageSize));
        return ResponseEntity.ok(result);
    }
}
